#########################################################################################
############################# Appendix (G) ##############################################
#########################################################################################

# Load the required packages
rm(list = ls())
library(fixest)
library(readstata13)
library(collapse)
library(dplyr)
library(stdidx)


#Loading the main dataset: 
# data_2rounds: Contains data on the 2 rounds of the election, excluding the pre-electoral period 
# data_3rounds: Contains data on the 2 rounds of the election, including the pre-electoral period as an additional round

data <- read_dta("data_2rounds.dta")
data3 <- read_dta("data_3rounds.dta")


#Create dictionary to present the names of the variables in the tables
dict=c("mb_dummy"="MB Running", "pct_urban"="Urban (%)", "pct_emp"="Employment (%)", 
       "sdeduscore"="Education (sd)","eduscore2"="Education2 (sd)", "gov_employed"="Gov. Employment (%)", "logprotest"="Protest (log)", 
       "highprofile"="High Profile", "logregistered"="Registered (log)", "female_pct"="Female (%)", "round2"="Runoff", 
       "votebuyingd"="Vote-buying", "fmean.female_pct"="Female", "fmean.logregistered"="Registered (log)", 
       "fmean.pct_urban"="Urban (%)", "fmean.pct_emp"="Employment (%)", "fmean.sdeduscore"="Education (sd)", 
       "fmean.gov_employed"="Gov. Employment (%)", "newndp"="New NDP", "split_ndp"="NDP Dissidents", 
       "incumbent"="Incumbents", "nocand_competing"="Candidates No.", "death_ndpaffil"="NDP Death", "client_index"="Clientelism", 
       "watani_1984" = "NDP 1984 (%)", "wafd_1984" = "MB Coalition 1984 (%)", "fmean.incumbent"="Incumbents", "fmean.nocand_competing"="Candidates No.", 
       "fmean.newndp"="New NDP", "fmean.split_ndp"="NDP Dissidents", "fmean.logprotest"="Protest (log)")


######### Table (18)

#First, we create logs of the main outcome (total violence)
data$logallintimid<- log(data$allintimid+1)
data3$logallintimid<- log(data3$allintimid+1)

#Then, we run the analysis for Table (18)

main1 <- feols(logallintimid ~ mb_dummy + newndp +  split_ndp + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
               + incumbent + nocand_competing + round2 | governorate, data)

main2 <- feols(logallintimid ~ mb_dummy + newndp +  split_ndp + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
               + incumbent + nocand_competing | governorate, subset(data, data$round==1))            

main3 <- feols(logallintimid ~ mb_dummy + newndp +  split_ndp + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
               + incumbent + nocand_competing | governorate, subset(data, data$round==2)) 

main4 <- feols(logallintimid ~ mb_dummy + newndp +  split_ndp + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
               + incumbent + nocand_competing+ round2+round1 | governorate, data3)            


etable(main1, main2, main3, main4, se = c("cluster"), cluster=~governorate, digits = 3, tex=T, sdBelow = T, 
       signifCode = c("***"=0.001, "**"=0.01, "*"=0.05, "+"=0.10), dict=dict, interaction.combine = "*")


######## Table (19)

main1 <- fepois(allintimid ~ mb_dummy + newndp +  split_ndp + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                + incumbent + nocand_competing + round2 | governorate, data)

main2 <- fepois(allintimid ~ mb_dummy + newndp +  split_ndp + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                + incumbent + nocand_competing | governorate, subset(data, data$round==1))            

main3 <- fepois(allintimid ~ mb_dummy + newndp +  split_ndp + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                + incumbent + nocand_competing | governorate, subset(data, data$round==2)) 

main4 <- fepois(allintimid ~ mb_dummy + newndp +  split_ndp + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                + incumbent + nocand_competing+ round2+round1 | governorate, data3)            


etable(main1, main2, main3, main4,  se = c("cluster"), cluster=~governorate, digits = 3, tex=T, sdBelow = T, 
       signifCode = c("***"=0.001, "**"=0.01, "*"=0.05, "+"=0.10), dict=dict, interaction.combine = "*")


############ Table (20)

main1 <- fenegbin(allintimid ~ mb_dummy + newndp +  split_ndp + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 , data)

main2 <- fenegbin(allintimid ~ mb_dummy + newndp +  split_ndp + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing , subset(data, data$round==1))            


main3 <- fenegbin(allintimid ~ mb_dummy + newndp +  split_ndp + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing , subset(data, data$round==2)) 

main4 <- fenegbin(allintimid ~ mb_dummy + newndp +  split_ndp + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing+ round2+round1, data3)            


etable(main1, main2, main3, main4, se = c("iid"), digits = 3, tex=T, sdBelow = T, 
       signifCode = c("***"=0.001, "**"=0.01, "*"=0.05, "+"=0.10), dict=dict, interaction.combine = "*")


########### Table (21)

main1 <- fenegbin(allintimid ~ mb_dummy + newndp +  split_ndp + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing | govround, data)


main3 <- fenegbin(allintimid ~ mb_dummy + newndp +  split_ndp + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing | govround, data3)            


etable(main1, main3,  se = c("cluster"), cluster=~govround, digits = 3, tex=T, sdBelow = T, 
       signifCode = c("***"=0.001, "**"=0.01, "*"=0.05, "+"=0.10), dict=dict, interaction.combine = "*")


########## Table (22)

#First, removing outliers
data_out <- subset(data, data$allintimid<=50)
data3_out <- subset(data3, data3$allintimid<=50)

#Then, running the analysis
main1 <- fenegbin(allintimid ~ mb_dummy + newndp +  split_ndp + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing + round2 | governorate, data_out)

main2 <- fenegbin(allintimid ~ mb_dummy + newndp +  split_ndp + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing | governorate, subset(data_out, data_out$round==1))            

main3 <- fenegbin(allintimid ~ mb_dummy + newndp +  split_ndp + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing | governorate, subset(data_out, data_out$round==2))     

main4 <- fenegbin(allintimid ~ mb_dummy + newndp +  split_ndp + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing+ round2+round1 | governorate, data3_out)            


etable(main1, main2, main3, main4,  se = c("cluster"), cluster=~governorate, digits = 3, tex=T, sdBelow = T, 
       signifCode = c("***"=0.001, "**"=0.01, "*"=0.05, "+"=0.10), dict=dict, interaction.combine = "*")


